package com.boot.es.rest;

import com.alibaba.fastjson.JSONObject;
import com.boot.es.entity.TestIndexDto;
import com.boot.es.entity.TestIndexEntity;
import com.boot.es.mapper.TestRepository;
import com.boot.es.service.TestService;
import com.boot.es.util.Utils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
public class TestRest {


    @Autowired
    public TestService testService;

    @Autowired
    private TestRepository testRepository;


    @GetMapping("/")
    public String index() {
        return  "hello";
    }



    // 增=改
    @PostMapping(value = "/test")
    public TestIndexEntity addSku(@RequestBody TestIndexEntity entity){
        try{
            TestIndexEntity e =   testRepository.save(entity);
            return e;
        }catch(Exception e){
            e.printStackTrace();
            return  null;
        }
    }


    // 删
    @DeleteMapping(value = "/test/{id}")
    public String deleteSku(@PathVariable Long id){
        try{
            testRepository.deleteById(id);
            return "删除成功";
        }catch(Exception e){
            e.printStackTrace();
            return  e.getMessage();
        }
    }

    @GetMapping("/test1")
    public  String test1() {
        Integer page_size = 20;
        Integer page_num = 0;
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        Pageable pageable =  new PageRequest(page_num,page_size,null);
        Page<TestIndexEntity> page = testRepository.search(boolQueryBuilder,pageable);
        return  JSONObject.toJSONString(page);
    }


    @GetMapping("/test")
    public  String test( @RequestParam String query,
                         String order_by,
                         Integer page_size,
                         Integer page_num) {

        page_num = (page_num == null || page_num < 1) ? 0 : page_num - 1;
        page_size = (page_size == null || page_size < 1) ? 20 : page_size;
        
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        Utils.generateQuery(query, TestIndexEntity.class, boolQueryBuilder);

        Pageable pageable = Utils.getPageable(order_by, page_size, page_num, TestIndexEntity.class);
        
        Page<TestIndexEntity> page = testRepository.search(boolQueryBuilder,pageable);

//        List<TestIndexEntity> list =  IteratorUtils.toList(page.iterator());
//        System.out.println(JSONObject.toJSONString(list));


        return  JSONObject.toJSONString(page);

    }



    // 增=改
    @GetMapping(value = "/add")
    public void addBatch(){
        try{
            for(int i = 1; i <= 10;i++) {
                String name = String.format("张三name%s搜索",i);
//                TestIndexEntity entity = new TestIndexEntity(Long.valueOf(i),String.valueOf(i),name ,i);
                TestIndexEntity entity = new TestIndexEntity();
                entity.setAge(i);
                entity.setId(Long.valueOf(i));
                entity.setName(name);
                entity.setNo("" + i + "");

                entity =  testService.add(entity);
                System.out.println(entity);
            }
        }catch(Exception e){
            e.printStackTrace();

        }
    }

    // 增=改
    @GetMapping(value = "/testQuery")
    public void testQuery(){
        try{
            TestIndexDto dto = new TestIndexDto();
            dto.setId(50L);
            dto.setNo("50");
            dto.setAge(50);
            dto.setName("张三name5");

            List<Long> list = new ArrayList<>();
            list.add(50L);
            list.add(51L);
            list.add(52L);

            dto.setId_in(list);

            testService.testQuery(dto);
        }catch(Exception e){
            e.printStackTrace();

        }
    }


    @GetMapping(value = "/and")
    public void testAnd(){
        try{


            TestIndexDto dto = new TestIndexDto();


            dto.setAge(50);
            dto.setName("name5");

            Iterable<TestIndexEntity> indexEntities = null;
            BoolQueryBuilder boolQueryBuilder =  null;
            QueryBuilder queryBuilder = null;
            Iterator iterator = null;

            // 查询年龄
            System.out.println(String.format("查询age<= %s的数据",dto.getAge()));
            queryBuilder = QueryBuilders.rangeQuery("age").lte(dto.getAge());
            boolQueryBuilder = new BoolQueryBuilder();
            boolQueryBuilder.must(queryBuilder);

            queryBuilder = QueryBuilders.wildcardQuery( "name","*" +  dto.getName() + "*");

            boolQueryBuilder.must(queryBuilder);



            indexEntities =  testRepository.search(boolQueryBuilder);
            iterator = indexEntities.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }



        }catch(Exception e){
            e.printStackTrace();

        }
    }




    @GetMapping(value = "/in")
    public void testIn(){
        try{

            Iterable<TestIndexEntity> indexEntities = null;
            BoolQueryBuilder boolQueryBuilder =  null;

            Iterator iterator = null;
            boolQueryBuilder = new BoolQueryBuilder();

            List<String> nos = new ArrayList<>();
            nos.add("1");
            nos.add("5");
            QueryBuilder qb = QueryBuilders.termsQuery("no",nos);

            boolQueryBuilder.must(qb);



            indexEntities =  testRepository.search(boolQueryBuilder);
            iterator = indexEntities.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }



        }catch(Exception e){
            e.printStackTrace();

        }
    }

}
